/*
    This file is part of XCOM Mod Manager.

    XCOM Mod Manager is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    XCOM Mod Manager is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with XCOM Mod Manager.  If not, see <http://www.gnu.org/licenses/>.
*/
/*
 * @author Anthony Surma
 */

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
 
public class ModXMLFile 
{
    final static String MODFILEEXTENSION = ".xcom_mod";
    final static String TEMPHASH = "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";
    
	void doStuff(ModMaker.ModData[] data, String modAuthor, String modDesc) throws ParserConfigurationException, TransformerConfigurationException, TransformerException, NoSuchAlgorithmException, IOException 
        { 	
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
            Element e;

            // root elements
            Document doc = docBuilder.newDocument();
            Element rootElement = doc.createElement("XCOMMod");
            doc.appendChild(rootElement);
            
            e = doc.createElement("ModHash");
            e.appendChild(doc.createTextNode(TEMPHASH));
            rootElement.appendChild(e);
            
            e = doc.createElement("ModName");
            e.appendChild(doc.createTextNode(data[0].modName));
            rootElement.appendChild(e);

            e = doc.createElement("ModAuthor");
            e.appendChild(doc.createTextNode(modAuthor));
            rootElement.appendChild(e);
            
            e = doc.createElement("ModDescription");
            e.appendChild(doc.createTextNode(modDesc));
            rootElement.appendChild(e);
                                   
            // Data elements
            for (int i = 0; i < data.length; ++i)
            {
            
                Element Data = doc.createElement("Data");
                rootElement.appendChild(Data);

                e = doc.createElement("UPKFilename");
                e.appendChild(doc.createTextNode(data[i].UPKFilename));
                Data.appendChild(e);

                e = doc.createElement("DataSum");
                e.appendChild(doc.createTextNode(Integer.toString(data[i].dataSum)));
                Data.appendChild(e);
                
                e = doc.createElement("SearchHash");
                e.appendChild(doc.createTextNode(data[i].searchHash));
                Data.appendChild(e);
                                
                e = doc.createElement("DiffData");
                e.appendChild(doc.createTextNode(data[i].getDiffString()));
                Data.appendChild(e);
            }
            
            // get file hash          
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            StreamResult result = new StreamResult(baos);
            transformer.transform(new DOMSource(doc), result);                                  
            byte[] b = baos.toByteArray();
            String s = getHash(b);            
            
            //write the hash
            b = writeHash(b, s);
            
            // write the xml file
            FileOutputStream fos = new FileOutputStream(new File(data[0].modName + "\\" + data[0].modName + MODFILEEXTENSION));            
            fos.write(b);
            fos.close();            
	}
        
        static byte[] writeHash(byte[] b, String hash)
        {
            String s = new String(b);
            
            s = s.replaceFirst(TEMPHASH, hash);         
            return s.getBytes();
        }
        
        static String getHash(byte[] b) throws NoSuchAlgorithmException, IOException
        {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(b, 0, b.length);
            return ModMaker.hashToString(md.digest());
    }
}
